package dk.ucn.datamatiker.henrik.bundtkage.earthcake.albeheweather;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class Downloader {
	private static Downloader instance;
	private static String LOG = "AlBeHeWeather";
	private Downloader() {

	}

	public static Downloader getInstance() {
		if (instance == null) {
			instance = new Downloader();
		}
		return instance;
	}
	
	// Downloads inputStream with URL.
	public InputStream downloadInputStream(String url) throws IOException {
		InputStream inputStream = null;

		try {
			URL downloadUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) downloadUrl
					.openConnection();
			conn.setReadTimeout(10000 /* milliseconds */);
			conn.setConnectTimeout(15000 /* milliseconds */);
			conn.setRequestMethod("GET");
			conn.setDoInput(true);
			// Starts the query
			conn.connect();
			int response = conn.getResponseCode();
			Log.d(LOG, "The response code is: " + response);
			inputStream = conn.getInputStream();
		}

		finally {
			if (inputStream != null) {
				inputStream.close();
			}
		}

		return inputStream;
	}
}
